{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-001\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = ()\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`x = ()`里的圆括号`()`表示创建一 tuple 元组，元组里的元素为空。所以 x 的类型就是元组 tuple。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-002\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# do not format\n",
    "x1 = 1\n",
    "x2 = 1  # x2 = (1)\n",
    "x3 = (1,)  # x3 = 1,\n",
    "x4 = (1,)\n",
    "print(x1, x2, x3, x4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`x2 = (1)` 里的圆括号表示:括号运算符。在这里是多余的，部分 python 开发工具会删除这个括号。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-003\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = 1, 2\n",
    "x2 = (\n",
    "    1,\n",
    "    2,\n",
    ")\n",
    "x3 = (1, 2)\n",
    "x4 = (\n",
    "    1,\n",
    "    2,\n",
    ")\n",
    "print(x1, x2, x3, x4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "x1、x2、x3 和 x4 都是一样\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-004\n",
    "\n",
    "> 如何使用元组交互变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 5\n",
    "a, b = b, a\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-005\n",
    "\n",
    "> 元组赋值给多个变量，元组元素多了或少了会怎样？\n",
    "\n",
    "```py\n",
    "a,b = b,a,c\n",
    "```\n",
    "\n",
    "```py\n",
    "a,b = c,\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = 5\n",
    "a, b = b, a, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = 5\n",
    "a, b = (c,)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-006\n",
    "\n",
    "> 把列表转元组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple([1, 2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-007\n",
    "\n",
    "> 拼接元组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = (1, 2)\n",
    "x2 = (3, 4, 5)\n",
    "\n",
    "\n",
    "x1 + x2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-008\n",
    "\n",
    "> 如何重复元组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sounds = (\"咚\", \"咚\", \"锵\")\n",
    "sounds * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-009\n",
    "\n",
    "> 如何判断两元组里的元素是否相同？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = (1, 2, 3)\n",
    "x2 = (1, 2, 3)\n",
    "x3 = (1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 == x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 == x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 != x3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-010\n",
    "\n",
    "> 如何判断两元组的包含关系？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = (1, 2, 3)\n",
    "x2 = (1, 2)\n",
    "x3 = (1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 > x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x2 < x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 >= x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 >= x3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-011\n",
    "\n",
    "> 如何修改元组\n",
    "\n",
    "元组不能修改。但可以把一个新元组赋值给这个变量。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-012\n",
    "\n",
    "> 元组字面量语法规则\n",
    "\n",
    "1. `(a,b,)`\n",
    "2. 空元素写成`()`\n",
    "3. 当元素个数大于 1 时，最后一个`,`可以省略。`(a,b)`是可以的，只有一个元素时必须写成`(a,)`。\n",
    "4. 当不是空元素，圆括号也可省写。`a,b` 或`a,b,`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TUP-013\n",
    "\n",
    "> 什么是元组解包 tuple unpack\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以把元组一次性给多个变量赋值。元组元素数量和变量数量必须一致。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
